﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using SynapseGaming.LightingSystem.Core;

namespace The_LarX
{
    public class Tree_Animated : Tree, IHasActor
    {
        public Actor Actor { get; set; }
        public sgMotionAnimatedActor TreeActor { get; set; }

        private string LeavesStartName;
        public Tree_Animated(string TextureName, string leavesStartName, string ModelName, 
            Renderer rend, GameScreen gs)
            : base(rend, gs)
        {
            string leaves = leavesStartName.ToLower();
            LeavesStartName = leaves;

            TreeActor = new sgMotionAnimatedActor(ModelName);
            Actor = TreeActor;
            TreeActor.ComponentParent = this;
            
            TreeActor.SubActors[1].DoubleSided = true;
            TreeActor.SubActors[1].TextureName = TextureName;
            TreeActor.SubActors[1].TransparencyMode = TransparencyMode.Clip;

            for (int i = 0; i < TreeActor.AnimationController.SkinnedBoneCustomRotations.Length; i++)
            {
                TreeActor.AnimationController.SkinnedBoneCustomRotations[i] = Vector3.Zero;
            }
            TreeActor.Update();
            TreeActor.Draw();
            TreeActor.OnDisable += new EventHandler(TreeActor_OnDisable);

            Renderer.SubmitActor(TreeActor);

            TreeActor.AddActorToRenderer = Renderer.SubmitActor;
            TreeActor.RemoveActorFromRenderer = Renderer.RemoveActor;
        }

        void TreeActor_OnDisable(object sender, EventArgs e)
        {
            this.DisableComponent();
        }
        public override void DisableComponent()
        {
            this.Actor.Disable();

            base.DisableComponent();
        }

        public Tree_Animated()
            : base()
        {
        }

        public override void Update()
        {
            base.Update();

            TreeActor.Update();
        }

        public override void Draw()
        {
            base.Draw();

            TreeActor.Draw();
        }

        public override void SerializeData()
        {
            base.SerializeData();

            Data.AddData("Tree.LeavesStartName", this.LeavesStartName);
            TreeActor.SerializeData(Data);
        }
        public override void DeserializeData()
        {
            base.DeserializeData();

            this.LeavesStartName = Data.GetData<string>("Tree.LeavesStartName");

            TreeActor = new sgMotionAnimatedActor();
            TreeActor.DeserializeData(Data);
            TreeActor.ComponentParent = this;
            this.Actor = TreeActor;

            TreeActor.SubActors[1].TransparencyMode = TransparencyMode.Clip;
            TreeActor.SubActors[1].Transparency = 1;

            for (int i = 0; i < TreeActor.AnimationController.SkinnedBoneCustomRotations.Length; i++)
            {
                TreeActor.AnimationController.SkinnedBoneCustomRotations[i] = Vector3.Zero;
            }

            Renderer.SubmitActor(TreeActor);

            TreeActor.Update();
            TreeActor.Draw();

            TreeActor.AddActorToRenderer = Renderer.SubmitActor;
            TreeActor.RemoveActorFromRenderer = Renderer.RemoveActor;

            foreach (var mesh in TreeActor.SceneObject.RenderableMeshes)
            {
                var bb = mesh.MeshBoundingBox;
                mesh.MeshBoundingBox = new BoundingBox(new Vector3(bb.Min.X, bb.Min.Z, -bb.Max.Y),
                                                      new Vector3(bb.Max.X, bb.Max.Z, -bb.Min.Y));
            }
            TreeActor.SceneObject.CalculateBounds();
        }

        public override ICloneable Clone()
        {
            Tree_Animated animated = new Tree_Animated(TreeActor.SubActors[1].TextureName,
                LeavesStartName, TreeActor.ModelName, Renderer, this.Parent);
            animated.Actor.Position = this.Actor.Position;
            animated.Actor.Scale = this.Actor.Scale;

            return animated;
        }
    }
}
